package redis

import (
	"fmt"
	"strconv"
)

func GetStock(shopId int) int64 {
	Num := "stock:shop_id_" + strconv.Itoa(shopId)
	return Rdb.LLen(Ctx, Num).Val()
}

func CreateCart(userId int, shopId int, cart map[string]interface{}) error {
	Num := "cart:user_id_" + strconv.Itoa(userId) + ":goods_id_" + strconv.Itoa(shopId)
	return Rdb.HMSet(Ctx, Num, cart).Err()
}

func FindCart(userId int, shopId int) bool {
	Num := "cart:user_id_" + strconv.Itoa(userId) + ":goods_id_" + strconv.Itoa(shopId)
	return Rdb.HExists(Ctx, Num, "shop_id").Val()
}

func UpdateCart(userId int, shopId int, stock int) error {
	Num := "cart:user_id_" + strconv.Itoa(userId) + ":goods_id_" + strconv.Itoa(shopId)
	return Rdb.HIncrBy(Ctx, Num, "goods_num", int64(stock)).Err()
}

func DelCart(userId int, shopId int) error {
	Num := "cart:user_id_" + strconv.Itoa(userId) + ":goods_id_" + strconv.Itoa(shopId)
	return Rdb.Del(Ctx, Num).Err()
}

func ClearCart(userId int) error {
	Num := "cart:user_id_" + strconv.Itoa(userId) + ":*"
	val := Rdb.Keys(Ctx, Num).Val()
	for _, s := range val {
		err := Rdb.Del(Ctx, s).Err()
		if err != nil {
			return err
		}
	}
	return nil
}

func ListCart(userId int) []string {
	Num := "cart:user_id_" + strconv.Itoa(userId) + ":*"
	return Rdb.Keys(Ctx, Num).Val()
}

func CartIsNilOrNotNil(userId int) bool {
	Num := "cart:user_id_" + strconv.Itoa(userId) + ":*"
	val := Rdb.Keys(Ctx, Num).Val()
	i := len(val)
	fmt.Println(i)
	if i == 0 {
		fmt.Println("val")
		return true
	}
	fmt.Println("aaa")
	return false
}

func SumCart(userId int) (int, float64) {
	Num := "cart:user_id_" + strconv.Itoa(userId) + ":*"
	val := Rdb.Keys(Ctx, Num).Val()
	var allSum int
	var allPrice float64
	for _, s := range val {
		m := Rdb.HGetAll(Ctx, s).Val()
		aToi, _ := strconv.Atoi(m["checked"])
		num, _ := strconv.Atoi(m["goods_num"])
		price, _ := strconv.ParseFloat(m["goods_price"], 64)
		if aToi == 1 {
			allSum += num
			allPrice += float64(num) * price
		}
	}
	return allSum, allPrice
}

func UpdateCartChecked(userId int, shopId int) error {
	Num := "cart:user_id_" + strconv.Itoa(userId) + ":goods_id_" + strconv.Itoa(shopId)
	val := Rdb.HGetAll(Ctx, Num).Val()
	check, _ := strconv.Atoi(val["checked"])
	if check == 1 {
		return Rdb.HSet(Ctx, Num, "checked", 0).Err()
	} else {
		return Rdb.HSet(Ctx, Num, "checked", 1).Err()
	}
}

func CheckedCart(userId int) error {
	Loc := "loc_"
	s1 := Rdb.Get(Ctx, Loc).Val()
	if s1 == "0" {
		Num := "cart:user_id_" + strconv.Itoa(userId) + ":*"
		val := Rdb.Keys(Ctx, Num).Val()
		for _, s := range val {
			err := Rdb.HSet(Ctx, s, "checked", 1).Err()
			if err != nil {
				return err
			}
		}
		Rdb.Set(Ctx, Loc, "0", 1)
	} else {
		Num := "cart:user_id_" + strconv.Itoa(userId) + ":*"
		val := Rdb.Keys(Ctx, Num).Val()
		for _, s := range val {
			err := Rdb.HSet(Ctx, s, "checked", 0).Err()
			if err != nil {
				return err
			}
		}
		Rdb.Set(Ctx, Loc, "0", 0)
	}
	/*Num := "cart:user_id_" + strconv.Itoa(userId) + ":*"
	val := Rdb.Keys(Ctx, Num).Val()
	for _, s := range val {
		m := Rdb.HGetAll(Ctx, s).Val()
		aToi, _ := strconv.Atoi(m["checked"])
		if aToi == 1 {
			err := Rdb.HSet(Ctx, s, "checked", 0).Err()
			if err != nil {
				return err
			}
		} else {
			err := Rdb.HSet(Ctx, s, "checked", 1).Err()
			if err != nil {
				return err
			}
		}
	}*/

	return nil
}
